using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisRange : AnalysisExpression
{
    internal AnalysisRange(RangeExpression expression, AnalysisNode? parent) : base(AnalysisBoundKinds.Range,
        expression, parent)
    {
        Syntax = expression;
    }

    public override RangeExpression Syntax { get; }
    
    public AnalysisExpression? Left { get; internal set; }
    
    public AnalysisExpression? Right { get; internal set; }


    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException($"AnalysisRange cannot have children, set {nameof(Left)} or {nameof(Right)} instead.");
    }

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return ((AnalysisExpression?[])[Left, Right]).OfType<AnalysisExpression>();
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    ///     创建一个区间表达式
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="left">左区间表达式</param>
    /// <param name="right">右区间表达式</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">当前表达式所在的With语句块</param>
    public static AnalysisRange Range(RangeExpression syntaxNode, AnalysisExpression left, AnalysisExpression right,
        AnalysisNode? parent, ITypeSymbol? withSymbol)
    {
        AnalysisRange range = new(syntaxNode, parent)
        {
            Left = left,
            Right = right,
        };
        
        return range.WithContainingWithSymbol(withSymbol);
    }
}